<?php

namespace App\Http\Controllers;

use App\Libs\Tools\DataTables;
use App\Libs\Tools\ResponseTools;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected function getPageLimit(){
        $page_size=DataTables::get_page_length();
        if(floor($page_size)!=$page_size || $page_size<1){
            $page_size=Config::get('repository.pagination.limit', 15);
        }
        return $page_size;
    }

    protected function return_json_success($data='success'){
        return response()->json(ResponseTools::create_success_response_json($data));
    }

    protected function return_json_error_message($errorMsg){
        return response()->json(ResponseTools::create_error_response_json($errorMsg));
    }

    protected function return_json_error($e){
        $errorMsg="";
        foreach ($e->getMessageBag()->all() as $err){
            $errorMsg=$errorMsg.$err.";";
        }
        return $this->return_json_error_message($errorMsg);
    }

    protected function wrap_user_info(array $all,$is_created_user_contain=false){
        $user = Auth::user();
        $all['updated_user']=$user['name'];
        if($is_created_user_contain){
            $all['created_user']=$user['name'];
        }
        return $all;
    }
}
